//
//  HDCFMycolecFundVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/2.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFMycolecFundVc.h"
#import "HDCFMycolcCell.h"
#import "SVProgressHUD.h"
#import "HDCFFundDetailVc.h"

#define KFundList @"/public/product/batch-get-fund-info"

@interface HDCFMycolecFundVc ()

@property (nonatomic, strong) NSMutableArray *myColecList;
@property (nonatomic, strong) NSArray *fundArray;

@end

@implementation HDCFMycolecFundVc

- (NSMutableArray *)myColecList{
    if (!_myColecList) {
        self.myColecList = [NSMutableArray array];
    }
    return _myColecList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"我的收藏"];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorColor = ZKcolor(245, 245, 245);
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getMycelet];
}

- (void)getMycelet{
    [SVProgressHUD show];
    [HttpRequest sendTwoSelfGetRequestWithURL:@"api/Favorite/GetList" WithParamets:nil success:^(id result) {
        NSNumber *code = result[@"code"];
        if ([code isEqualToNumber:@1]) {
            NSArray *array = result[@"result"];
            NSMutableArray *fundCodeArray = [NSMutableArray array];
            if (array.count > 0) {
                for (int i = 0; i < array.count; i++) {
                    NSDictionary *dict = array[i];
                    [fundCodeArray addObject:dict[@"FundCode"]];
                }
                NSString *fundCodes = [fundCodeArray componentsJoinedByString:@","];
                [self getDataOneWithFundCodes:fundCodes];
            }else{
                [SVProgressHUD showInfoWithStatus:@"您还未收藏"];
                [SVProgressHUD dismissWithDelay:1.0];
            }
            
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (void)getDataOneWithFundCodes:(NSString *)fundCodes{
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"fundCodes"] = fundCodes;
    paramets[@"pageSize"] = @1000;
    paramets[@"pageIndex"] = @1;
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{@"paramList":jsonStr,
                              @"methodPath":KFundList};
    
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            [SVProgressHUD dismiss];
            NSDictionary *dataDict = result[@"data"];
            self.fundArray = dataDict[@"data"];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.fundArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    HDCFMycolcCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFMycolcCell HDCFMycolcCell];
    }
    NSDictionary *dict = self.fundArray[indexPath.row];
    cell.dataDict = dict;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HDCFFundDetailVc *vc = [[HDCFFundDetailVc alloc] init];
    vc.isColect = YES;
    vc.dataDict = self.fundArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 112;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
